package com.ps.actions.setting;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.reflect.TypeToken;
import com.ps.actions.BaseAction;
import com.ps.entity.Result;
import com.ps.pojo.setting.Role;
import com.ps.service.setting.RoleService;
import com.ps.service.setting.imp.RoleServiceImp;
import com.ps.util.Kit;
import com.ps.util.PaginationSupport;
import com.sun.java.swing.plaf.windows.resources.windows;

public class RoleAction extends BaseAction {
	private static final Logger log = LoggerFactory
		.getLogger(RoleAction.class);
	
	@Autowired
	private RoleService roleService;
	
    private String roleJson;
    
    private String permissionIdsJson;
    
    private String paginationJson;
	
    private Integer roleId;
    
    private Role role;
    
	public String  addRole() {
/*		System.out.println(this.roleJson);
		System.out.println(this.permissionIdsJson);*/
		Role role = Kit.getFromJson(roleJson, Role.class);
		role.setInputerId(1);
		role.setInTime(new Timestamp(System.currentTimeMillis()));
		Type type = new TypeToken<List<Integer>>(){}.getType(); 
		List<Integer> permissionIds = Kit.getFromJson(permissionIdsJson, type);
		Result result = roleService.addRole(role, permissionIds);
		this.outputJson(Kit.toJson(result));
		return null;
	}

	@SuppressWarnings("unchecked")
	public void findByPage() {
		System.out.println("paginationJson = " + paginationJson);
		PaginationSupport paginationSupport = Kit.getFromJson(paginationJson, PaginationSupport.class);
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Role.class);
		Result result = roleService.findPageByCriteria(detachedCriteria, paginationSupport.getPageSize(), paginationSupport.getStartIndex(), Order.desc("roleId"));
		System.out.println(Kit.toJson(result));
		this.outputJson(Kit.toJson(result));
	}
	
	public String jumpToUpdateRole() {
		System.out.println("roleId = " + getRoleId());
		setRole((Role) roleService.findById(roleId).getDatas());
		return "updateRolePage";
	}
	
	public String jumpToAdd() {
		return "addRolePage";
	}
	
	
	public void updateRole() {
		System.out.println(this.roleJson);
		Role role = Kit.getFromJson(roleJson, Role.class);
		role.setInputerId(1);
		role.setInTime(new Timestamp(System.currentTimeMillis()));
		Type type = new TypeToken<List<Integer>>(){}.getType(); 
		List<Integer> permissionIds = Kit.getFromJson(permissionIdsJson, type);
		Result result = roleService.updateRole(role, permissionIds);
		this.outputJson(Kit.toJson(result));
	}
	
	public void deleteRole() {
		Result result = roleService.delete(roleId);
		this.outputJson(Kit.toJson(result));
	}
	
	public void queryRoleDetail() {
		Result result = roleService.queryRoleDetail(roleId);
		System.out.println(Kit.toJson(result));
		this.outputJson(Kit.toJson(result));
	}
	
	public void setRoleJson(String roleJson) {
		this.roleJson = roleJson;
	}

	public String getRoleJson() {
		return roleJson;
	}

	public void setPermissionIdsJson(String permissionIdsJson) {
		this.permissionIdsJson = permissionIdsJson;
	}

	public String getPermissionIdsJson() {
		return permissionIdsJson;
	}

	public void setPaginationJson(String paginationJson) {
		this.paginationJson = paginationJson;
	}

	public String getPaginationJson() {
		return paginationJson;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Role getRole() {
		return role;
	}



    


	
}
